include("${CMAKE_CURRENT_LIST_DIR}/RealmTargets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/AcquireRealmDependency.cmake")

# Find dependencies
include(CMakeFindDependencyMacro)

if(@REALM_HAVE_OPENSSL@)
    if(NOT @REALM_USE_SYSTEM_OPENSSL@ AND (ANDROID OR WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux"))
        # Use our own prebuilt OpenSSL
        realm_acquire_dependency(openssl @OPENSSL_VERSION@ OPENSSL_CMAKE_INCLUDE_FILE)

        include(${OPENSSL_CMAKE_INCLUDE_FILE})
    endif()
    find_dependency(OpenSSL @OPENSSL_VERSION@)
endif()

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_dependency(Threads)

# Use Zlib for Sync, but allow integrators to override it
# Don't use find_library(ZLIB) on Apple platforms - it hardcodes the path per platform,
# so for an iOS build it'll use the path from the Device plaform, which is an error on Simulator.
# Just use -lz and let Xcode figure it out
if(TARGET Realm::Sync AND NOT APPLE AND NOT TARGET ZLIB::ZLIB)
    if(WIN32 OR (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND REALM_LINUX_TOOLCHAIN))
        find_package(ZLIB)
        if (NOT ZLIB_FOUND)
            realm_acquire_dependency(zlib @DEP_ZLIB_VERSION@ ZLIB_CMAKE_INCLUDE_FILE)
            include(${ZLIB_CMAKE_INCLUDE_FILE})
        endif()
    elseif(ANDROID)
        # On Android FindZLIB chooses the static libz over the dynamic one, but this leads to issues
        # (see https://github.com/android/ndk/issues/1179)
        # We want to link against the stub library instead of statically linking anyway,
        # so we hack find_library to only consider shared object libraries when looking for libz
        set(_CMAKE_FIND_LIBRARY_SUFFIXES_orig ${CMAKE_FIND_LIBRARY_SUFFIXES})
        set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
    endif()
    find_dependency(ZLIB)
    if(ANDROID)
        set(CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES_orig})
    endif()
endif()